<?php

namespace app\api\controller;


use app\api\model\scenic\Scenic;
use app\api\model\scenic\Scenic as ScenicModel;
use app\api\model\scenic\ScenicCategory;
use app\api\model\scenic\ScenicEvaluateComment;
use app\api\model\scenic\ScenicEvaluateZan;
use app\api\model\scenic\ScenicTickets;
use app\common\controller\Api;
use app\api\model\scenic\ScenicEvaluate as ScenicEvaluateModel;
use think\Db;

class Scenicevaluate extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['lists'];

    //点评排名
    public function ranking()
    {

        $page_size = input('page_size',10);
        $city_id = input('city_id',0);

        $where = ['status'=>1];
        /*if ($city_id){
            $where['city_id'] = $city_id;
        }*/

        $list = ScenicModel::where($where)
            ->field('scenic_id,name,image,category_id,level,score,intro,lng,lat,city_id,comment_num')
            ->order('comment_num desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        foreach ($list as $k => $item) {
            $tickets_list = ScenicTickets::where(['scenic_id'=>$item['scenic_id']])->column('price');
            $list[ $k ][ 'start_price' ] = count($tickets_list)>0?min($tickets_list):0;
        }

        $this->success('',$list);
    }

    //我的点评
    public function myEvaluate()
    {
        $page_size = input('page_size',10);

        $where = ['ge.user_id'=>$this->auth->id];

        $list = ScenicEvaluateModel::alias('ge')
            ->join('scenic s','ge.scenic_id = s.scenic_id')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,s.name,s.image,s.category_id,s.level,s.score,s.intro,s.lng,s.lat,s.city_id,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        $scenic_model = new Scenic();
        $level_list = $scenic_model->getLevelList();
        foreach ($list as $k=>$v){
            $category_list = ScenicCategory::where('id','in',$v['category_id'])->column('name');
            $list[$k]['category_id_text'] = $category_list;
            $list[$k]['level_text'] = $level_list[$v['level']];
        }
        $this->success('',$list);
    }

    //评价列表
    public function lists()
    {
        $page_size = input('page_size',10);
        $scenic_id = input('scenic_id',0);

        $where = ['ge.scenic_id'=>$scenic_id];

        $list = ScenicEvaluateModel::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);
        foreach ($list as $k=>$v){
            $info = ScenicEvaluateZan::where(['evaluate_id'=>$v['evaluate_id'], 'user_id' => $this->auth->id])->find();
            $list[$k]['is_zan'] = $info?1:0;
            $list[$k]['comment_num'] = ScenicEvaluateComment::where(['evaluate_id'=>$v['evaluate_id']])->count();
        }
        $this->success('',$list);
    }

    //提交评价
    public function add()
    {
        try {
            $model = new ScenicEvaluateModel();
            $param = input();
            $param['user_id'] = $this->auth->id;
            $res = $model->add($param);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('提交成功',$res);

    }

    //评论评价
    public function comment()
    {
        $comment_model = new ScenicEvaluateComment();

        $data = [
            'user_id'=>$this->auth->id,
            'evaluate_id'=>input('evaluate_id',0),
            'content'=>input('content',''),
        ];

        $info = ScenicEvaluateModel::get($data['evaluate_id']);

        if (empty($info))$this->error('评价不存在');

        if (empty($data['evaluate_id']))$this->error('缺少参数评价id');

        if (empty($data['content']))$this->error('请输入评论内容');

        $res = $comment_model->save($data);

        $this->success('评论成功');
    }

    //评论列表
    public function commentlists()
    {
        $page_size = input('page_size',10);
        $evaluate_id = input('evaluate_id',0);

        $where = ['ge.user_id'=>$this->auth->id,'ge.evaluate_id'=>$evaluate_id];

        $list = ScenicEvaluateComment::alias('ge')
            ->join('user u','ge.user_id = u.id')
            ->where($where)
            ->field('ge.*,u.nickname,u.avatar')
            ->order('ge.createtime desc')
            ->paginate($page_size, false, ['query' => request()->get()]);

        $this->success('',$list);

    }
}